#!/bin/bash
# Script: pi-temp.sh
# This should work on both arch and ubuntu/debian
# Purpose: Display the ARM CPU and GPU temperatures of Raspberry Pi 2/3/4/Zero
# Edited by: srepac <srepac@kvmnerds.com>
# convert C to F and show both temps in output
# -------------------------------------------------------
# install basic calculator for conversions
if [ `which bc | wc -l` -le 0 ]; then
        sudo apt-get install bc -y
fi

    MODEL=$(tr -d '\0' </proc/device-tree/model)

    # code section to get Pi RAM size
    RAMGB=$( sudo vcgencmd get_config total_mem | awk -F= '{NUM = $2} END {
  if ( NUM < 1024 ) print NUM "MB";
  else print (NUM / 1024) "GB"; }' )

    AVAIL=$( free -m | grep Mem | awk '{print $NF}' )

    printf "$MODEL ${RAMGB}  Hostname: $(hostname)\n$(date)  Load average:$(uptime | awk -F: '{print $NF}')  Avail RAM: ${AVAIL}MB\n"
    GPUTEMP=`sudo $(which vcgencmd) measure_temp | awk -F= '{print $2}' | sed "s/'C//g"`
    CPU=$(</sys/class/thermal/thermal_zone0/temp)

    cpuC=`echo "scale=3; $CPU / 1000" | bc`
    cpuF=`echo "scale=2; 9/5 * ${cpuC} + 32" | bc`
    gpuF=`echo "scale=2; 9/5 * ${GPUTEMP} + 32" | bc`

    #echo "$(date) @ $(hostname)"
    echo "-------------------------------------------"
    printf "CPU => ${cpuC}'C\t${cpuF}'F\n"
    printf "GPU => ${GPUTEMP}'C\t${gpuF}'F\n"

    MHZ=`sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq`
    #echo "CPU0 MHZ: $((MHZ/1000))"

    #echo "CPU (All) MHz: "
    count=0
    for i in `sudo cat /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_cur_freq`; do
        cpu=`echo "scale=2; $i / 1000" | bc`
        echo "CPU${count} MHz:  $cpu"
        count=`expr $count + 1`
    done

    sudo vcgencmd measure_volts | sed 's/volt=/vCore /g'
    sudo vcgencmd get_throttled
